用 cgroup 控制 CPU占用率 您所在的位置:网站首页 cgroup cpu share 用 cgroup 控制 CPU占用率

用 cgroup 控制 CPU占用率

2024-07-16 11:57| 来源: 网络整理| 查看: 265

用 cgroup 控制 CPU占用率

使用参考1 【note】linux上限制进程cpu占用率

使用参考2 Linux---使用 nice、cpulimit 和 cgroups 限制 cpu 占用率

使用参考3 cgroups介绍及安装配置使用详解

理论参考1 Linux资源管理之cgroups简介

以下内容摘自或参考以上博文 介绍

Linux下的cgroups,全称叫Control Groups,最初由Google工程师提出并实现,linux从2.6开始将这个特性纳入内核。cgroups主要对进程按组(Group)进行资源配额的控制,包括CPU、内存、IO等,相比古老的ulimit,cgroups更为平滑和易用,成为容器技术(比如docker)的基础。

安装 libcgroup

libcgroup:一个开源软件,提供了一组支持cgroups的应用程序和库,方便用户配置和使用cgroups。目前许多发行版都附带这个软件。

安装方法:

$ sudo yum install libcgroup 配置 cgroup 之前需要先创建并挂载 cgroup 子系统 // 创建文件夹用于存放 cgroup 子系统 $ sudo mkdir -p /cgroup/mave // 挂载 cgroup 子系统至创建的文件夹中 $ sudo mount -t cgroup -o cpu mave /cgroup/mave // 查看挂载的子系统 $ sudo cat /proc/mounts 配置 cgconfig 文件,已建立控制CPU,内存等的子系统

cgconfig 语法格式:

[] { = ; … } …} name: 指定cgroup的名称 permissions:可选项,指定cgroup对应的挂载点文件系统的权限,root用户拥有所有权限。 controller:子系统的名称 param name 和 param value:子系统的属性及其属性值

具体内容:

// 打开 cgconfig 配置文件 $ sudo vim /etc/cgconfig.conf

写入如下内容:

# # Copyright IBM Corporation. 2007 # # Authors: Balbir Singh # This program is free software; you can redistribute it and/or modify it # under the terms of version 2.1 of the GNU Lesser General Public License # as published by the Free Software Foundation. # # This program is distributed in the hope that it would be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # # # By default, we expect systemd mounts everything on boot, # so there is not much to do. # See man cgconfig.conf for further details, how to create groups # on system boot using this file. # # 控制用户(或用户组)cpu使用比例的子系统, # cpu.cfs_quota_us:表示当前这个组中的task(/cgroup/mave/tasks中的taskid) # 将分配多少比例的cpu时间片,由于服务器有56个核,因此最大值为5600000 # (即 cpu.cfs_quota_us = 56 * cpu.cfs_period_us)。 # cpu.cfs_period_us:表示将cpu时间片分成100000份(100000 为默认值)。 # 如下 cpu.cfs_quota_us = 4000000, 表示该子系统允许用户(或用户组)使用最 # 多40个核。 group user { cpu { cpu.cfs_quota_us = 4000000; cpu.cfs_period_us = 100000; } } # 控制各用户(或用户组)之间对cpu使用的分配比例。 # cpu.shares 属性用于控制 cpu 在各用户(或用户组)之间的分配比例。它的默认 # 值是 1024。 # 如下 cpu.shares=512, 表示该子系统下用户(或用户组)在与其他用户争夺资源 # 时,分配到的cpu使用比例是其他用户(或用户组,cpu.shares=1024)的一半,即 # (分配比 = 512 / 1024 = 1/2) group ghalf { cpu { cpu.shares=512; } } 配置 cgrules 文件,将cgroup 子系统应用到用户(或用户组上)

内容如下:

# /etc/cgrules.conf #The format of this file is described in cgrules.conf(5) #manual page. # # Example: # #@student cpu,memory usergroup/student/ #peter cpu test1/ #% memory test2/ # End of file *:iscsid net_prio cgdcb-4-3260 * cpu user/ # 控制所有用户最大使用 40 个核(子系统 user) @group1 cpu ghalf/ # 控制用户组cpu分配比为正常的1/2 (子系统 ghalf) @group2 cpu ghalf/ @group3 cpu ghalf/ 启动 cgroup 服务使配置生效和配置自启 $ sudo systemctl enable cgconfig.service $ sudo systemctl start cgconfig.service $ chkconfig cgconfig on $ sudo systemctl enable cgred.service $ sudo systemctl start cgred.service $ chkconfig cgred on

或者(低版本centos中:)

$ sudo service cgconfig restart $ sudo chkconfig cgconfig on $ sudo service cgred start $ sudo chkconfig cgred on


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有